package com.neo.sk.collector

import akka.actor.Actor
import com.neo.sk.common.{SaveMovieDetail, SaveFinishTagUrl, SaveUrlList, SaveTagList}
import com.neo.sk.models.{MovieSimpleDetail, MovieFinishTag, MovieUrl, MovieTag}
import models.Tables.{MoFinishTagRow, MoUrlRow, MoTagRow}
import org.slf4j.LoggerFactory

import scala.concurrent.Future

/**
 * User: huangshanqi
 * Date: 2015/6/29
 * Time: 19:47
 */
class SaveActor extends Actor{

  private val logger = LoggerFactory.getLogger(this.getClass)
  import Manager.myExecutionContext

  override def receive: Receive = {

    case SaveTagList(tags) =>{
      Future{
        val tagRows = tags.map{
          tuple =>
            MoTagRow(0,tuple._2,tuple._1)
        }.toList
        MovieTag.batchInsert(tagRows)
      }
    }
    case SaveUrlList(urls,tagName) =>{
      Future{
        val urlRows = urls.map{
          tuple =>
            MoUrlRow(0,tuple._1,tuple._2,tagName)
        }.toList
        MovieUrl.batchInsert(urlRows)
      }
    }
    case SaveFinishTagUrl(url,tagName) =>{
      Future{
        MovieFinishTag.add(MoFinishTagRow(0,url,tagName))
      }
    }
    case SaveMovieDetail(movie) =>{
      Future{
        MovieSimpleDetail.insert(movie)
      }
    }
    case unknow@_ => logger.info("unknow message: "+ unknow + "  in " + context.self.path.name + " from " + context.sender().path.name)
  }
}
